package cn.ggy.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {

    private static SqlSessionFactory factory;

    static{
        try {
            //获取流
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            //创建工厂 开始建造
            factory = new SqlSessionFactoryBuilder().build(is);
            is.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
   }

   //获取SqlSession
    public static SqlSession getSession(){

        return factory.openSession();
    }

    //释放资源
    public static void close(SqlSession sqlSession){
        if (sqlSession!= null){
            sqlSession.close();
        }
    }

}
